<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2018/11/28
 * Time: 9:56
 */

namespace app\api\controller;

use app\common\logic\Logic;
use app\common\logic\UserFollow;
use \app\common\model\UserFollow as model;
use app\common\model\UserFriend;
use think\Db;
use think\Exception;

class Follow extends User
{

    
    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 关注用户
     */
    public function follow()
    {
        $id = dehashid(input('post.id'));

        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }
        if ($id == $this->user_id){
            api_return(0,'您不能关注自己');
        }

        $map['follow_user'] = $id;
        $map['user_id']     = $this->user_id;

        $model = new UserFollow();

        $data = $model->get($map);

        //查询我关注的用户是否关注了我  如果关注了 type改为1
        $type['follow_user'] = $this->user_id;
        $type['user_id']     = $id;
        $type['status']      = 1;
        $follow = $model->get($type);
        if ($data){
            if ($data['status'] == 1) api_return(0,'您已关注该用户,请勿重复操作');
            $next = $data;
        }else{
            $next = $model;
        }
        $map['status'] = 1;
        Db::startTrans();
        try{
            if ($follow){
                $map['type']  = 1;
                $follow->save(['type'=>1]);
            }
            $next->save($map);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            api_return(0,$e->getMessage());
        }
        api_return(1,'关注成功');

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 取消关注
     */
    public function cancel()
    {
        $id = dehashid(input('post.id'));
        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }
        $map['follow_user'] = $id;
        $map['user_id']     = $this->user_id;
        $model = new UserFollow();
        $data = $model->get($map);
        if ($data && $data['status'] == 1){

            Db::startTrans();
            try{
                //如果对方和我互为好友  取消好友关系
                if ($data['type'] == 1){
                    $type['follow_user'] = $this->user_id;
                    $type['user_id']     = $id;
                    $model->where($type)->update(['type'=>0]);
                }
                $save['status'] = 0;
                $save['type']   = 0;
                $data->save($save);
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                api_return(0,'服务器繁忙,请稍后重试',$e->getMessage());
            }
            api_return(1,'取消关注成功');
        }else{
            api_return(0,'您未关注该用户!');
        }
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取我的粉丝
     */
    public function fans()
    {

        $model = new model();

        $map['a.status'] = 1;
        $map['a.follow_user'] = $this->user_id;

        $rows = $model->getRows($map);

        api_return(1,'获取成功',$rows);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取我的关注
     */
    public function following()
    {
        $model = new model();

        $map['a.status']  = 1;
        $map['a.user_id'] = $this->user_id;
        if(input('keyword')){
            $map['u.nick_name|u.phone|u.uuid'] = ['like','%'.trim(input('keyword')).'%'];
        }
        $rows = $model->getFollow($map);

        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取我的好友
     */
//    public function friends()
//    {
//        $model = new model();
//
//        $map['a.status']  = 1;
//        $map['a.follow_user'] = $this->user_id;
//        $map['a.type']    = 1;
//
//        $rows = $model->getRows($map);
//
//        api_return(1,'获取成功',$rows);
//    }

    /**
     * 获取我的好友
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function friends()
    {
        $model = new UserFriend();

        $map['a.status']  = 1;
        $map['a.follow_user'] = $this->user_id;
        $map['a.type']    = 1;

        $rows = $model->getRows($map,$this->user_id);

        api_return(1,'获取成功',$rows);
    }


    /**
     * 搜索好友和粉丝
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function searchFriendAndFans()
    {

        $keyword = input('keyword');
        if (empty($keyword)){
            api_return(0,'请输入要搜索的内容');
        }
        $fansMap['a.status']           = 1;
        $fansMap['a.user_id']          = $this->user_id;
        $fansMap['u.nick_name|u.uuid'] = ['like',trim($keyword).'%'];
        $rows['fans'] = model::search($fansMap);

        $map['a.status']           = 1;
        $map['a.follow_user']      = $this->user_id;
        $map['a.type']             = 1;
        $map['u.nick_name|u.uuid'] = ['like',trim($keyword).'%'];

        $rows['friend'] = UserFriend::search($map);
        api_return(1,'获取成功',$rows);
    }



    /***
     * 申请添加好友
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function friendApply()
    {
        $id = dehashid(input('post.id'));
        if (!is_numeric($id)){
            api_return(0,'参数错误');
        }

        if ($id == $this->user_id){
            api_return(0,'您不能添加自己为好友');
        }

        //检查是否有好友申请
        $map['user_id']     = $this->user_id;
        $map['follow_user'] = $id;
        $has = UserFriend::where($map)->find();
        if ($has){
            switch ($has['status']){
                case 2:
                    api_return(0,'您已发送好友申请请等待对方同意');
                    break;
                case 1:
                    api_return(0,'您和对方已经是好友了,请勿重复添加');
                    break;
                default:
                   break;
            }

            $reuslt = $has->save(['status'=>2]);

        }else{

            $map['status'] = 2;
            $map['create_time'] = time();
            $map['update_time'] = time();
            $result = UserFriend::insert($map);

        }

        if ($result !== false){
            api_return(1,'申请提交成功,请耐心等待对方同意');
        }

    }


    /**
     * 好友申请列表
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function applyList()
    {
        $map['a.follow_user'] = $this->user_id;
        $map['a.status']      = ['in',[1,2,3]];
        $rows = UserFriend::alias('a')
            ->where($map)
            ->join('users u','u.user_id = a.user_id')
            ->field('a.id as follow_id,a.user_id,u.nick_name,a.status,u.header_img,u.sex,a.status')
            ->order('a.update_time desc')
            ->select();
        api_return(1,'获取成功',$rows);
    }

    /**
     * 同意好友申请
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function agreeApply()
    {
        $id = dehashid(input('post.id'));
        if (!is_numeric($id)){
            api_return(0,'参数错误');
        }

        //检查是否有好友申请
        $map['user_id']     = $id;
        $map['follow_user'] = $this->user_id;
        $map['status']      = 2;
        $data = UserFriend::where($map)->find();
        if (!$data){
            api_return(0,'参数错误');
        }

        if (input('type') == 'refuse'){
            $status = 3;
        }else {
            $status = 1;
        }

        $data->save(['status'=>$status]);
        if ($status == 1) {
            $item['user_id'] = $this->user_id;
            $item['follow_user'] = $id;
            $item['type'] = 1;
            $item['create_time'] = time();
            $item['update_time'] = time();
            UserFriend::insert($item);
        }
        api_return(1,'操作成功');

    }

    /**
     * 删除好友
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function delete()
    {

        $id = dehashid(input('post.id'));

        if (!is_numeric($id)){
            api_return(0,'参数错误');
        }

        //检查是否有好友申请
        $map['user_id']     = $id;
        $map['follow_user'] = $this->user_id;
        $map['status']      = 1;
        $data = UserFriend::where($map)->find();
        if (!$data){
            api_return(0,'参数错误');
        }

        $data->save(['status'=>0]);
        $map['follow_user'] = $id;
        $map['user_id'] = $this->user_id;
        $map['status']  = 1;
        UserFriend::where($map)->update(['status'=>0]);
        api_return(1,'操作成功');
    }



    
    public function test()
    {
        $map['follow_user'] = $this->user_id;
        $map['user_friend.status']      = ['in',[1,2,3]];
        $model = new UserFriend();
        $rows = $model
            ->with(['user'])
            ->where($map)

            ->select();
//        foreach ($rows as $k => $v){
//            $v->user;
//        }
        api_return(1,'获取成功',$rows);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取好友分组
     */
    public function friendsGroup()
    {
        $model = new model();
        $map['a.status']  = 1;
        $map['a.follow_user'] = $this->user_id;
        $map['a.type']    = 1;
        $rows = $model->getGroup($map);
        api_return(1,'获取成功',$rows);
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 我的黑名单列表
     */
    public function blackList()
    {
        $model = new model();

        $map['a.status']  = 2;
        $map['a.user_id'] = $this->user_id;

        $rows = $model->getRows($map);

        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 拉黑
     */
    public function pullBlack()
    {
        $id = dehashid(input('post.id'));

        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }
        if ($id == $this->user_id){
            api_return(0,'您不能拉黑自己');
        }

        $map['follow_user'] = $id;
        $map['user_id']     = $this->user_id;

        $model = new UserFollow();

        $data = $model->get($map);

        //查询拉黑的用户是否和我互相关注  如果关注了 type改为0
        $type['follow_user'] = $this->user_id;
        $type['user_id']     = $id;
        $type['status']      = 1;
        $follow = $model->get($type);
        if ($data){
            if ($data['status'] == 2) api_return(0,'您已拉黑该用户,请勿重复操作');
            $next = $data;
        }else{
            $next = $model;
        }
        $map['status'] = 2;
        Db::startTrans();
        try{
            if ($follow && $follow['type'] == 1){
                $map['type']  = 0;
                $follow->save(['type'=>0]);
            }

            $next->save($map);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            api_return(0,$e->getMessage());
        }
        api_return(1,'拉黑成功');
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 取消拉黑
     */
    public function removeBlack()
    {
        $id = dehashid(input('post.id'));
        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }
        $map['follow_user'] = $id;
        $map['user_id']     = $this->user_id;
        $model = new UserFollow();
        $data  = $model->get($map);
        if ($data && $data['status'] == 2){
            Db::startTrans();
            try{

                $save['status'] = 0;
                $save['type']   = 0;
                $data->save($save);
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                api_return(0,'服务器繁忙,请稍后重试',$e->getMessage());
            }
            api_return(1,'移除成功');
        }else{
            api_return(0,'您未拉黑该用户!');
        }
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 关注房间
     */
    public function roomFollow()
    {
        $id = input('post.id');

        $roomInfo = $this->roomInfo($id);

        if (!$roomInfo) api_return(0,'房间id错误');
        if ($roomInfo['user_id'] == $this->user_id){
            api_return(0,'您不能关注自己的房间');
        }

        $map['room_id'] = $id;
        $map['user_id'] = $this->user_id;

        $model = new Logic();
        $model->changeTable('room_follow');

        $data = $model->where($map)->find();

        if ($data && $data['status'] != 0){
            api_return(0,'您已关注该房间');
        }

        if ($data){

            $result = $data->save(['status'=>1]);

        }else{
            $result =$model->save($map);
        }

        if ($result){
            api_return(1,'关注成功');
        }

        api_return(0,$data->getError().$model->getError());

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 取消关注
     */
    public function roomCancel()
    {
        $id = input('post.id');
        $map['room_id'] = $id;
        $map['user_id'] = $this->user_id;
        $model = new Logic();
        $model->changeTable('room_follow');
        $data = $model->where($map)->find();
        if (!$data || $data['status'] == 0){
            api_return(0,'您未关注该房间');
        }
        if ($data['status'] != 1){
            api_return(0,'管理员不能取消关注');
        }
        $result =  $data->save(['status'=>0]);

        if ($result) api_return(1,'取消关注成功');
        api_return(0,'操作失败');
    }



}